package com.iris.live.services.report.sources;

import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.data.models.CrmTopicModel;
import com.iris.live.services.data.repositories.CRMRepository;
import com.iris.live.services.data.repositories.CrmTopicRepository;
import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/1/6.
 */
@Component("ExportGroupSurveyResultSource")
public class ExportGroupSurveyResultSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDealerSurveyResultSource.class);

    @Autowired
    private CRMRepository crmRepository;

    @Autowired
    private CrmTopicRepository crmTopicRepository;

    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = null;
        String[] areas = null;
        String version = context.get("version") == null ? null : context.get("version").toString();
        if (version == null || "".equals(version)) {
            String connectDateStart = context.get("connectDateStart") == null ? null : context.get("connectDateStart").toString();
            String connectDateEnd = context.get("connectDateEnd") == null ? null : context.get("connectDateEnd").toString();
            String area = context.get("area") == null ? null : context.get("area").toString();
            if (area != null && !"".equals(area)) {
                areas = area.split(",");
            }
            List list = crmRepository.findVersionForGroupAboutHistory(areas, connectDateStart, connectDateEnd);
            if (list != null && !list.isEmpty()) {
                sheets = new Sheet[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    Map map = (Map) list.get(i);
                    sheets[i] = new Sheet(map.get("groupVersion").toString(), map.get("groupVersion").toString(), true);
                }
            }
        } else {
            sheets = new Sheet[1];
            sheets[0] = new Sheet(version, version, true);
        }
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String[] areas = null;
        String version = sheet.getCode();
        String area = context.get("area") == null ? null : context.get("area").toString();
        if (area != null && !"".equals(area)) {
            areas = area.split(",");
        }
        String connectDateStart = context.get("connectDateStart") == null ? null : context.get("connectDateStart").toString();
        String connectDateEnd = context.get("connectDateEnd") == null ? null : context.get("connectDateEnd").toString();
        List values = getSurveyResult(areas, version, connectDateStart, connectDateEnd);

        List<Row> list = new ArrayList<>();

        list.add(new Row());
        list.add(new Row());
        list.add(new Row());

        Cell[] cells = new Cell[4];
        cells[0] = new Cell(sheet.getCode(), 2);
        cells[1] = new Cell("集团", 5);
        cells[2] = new Cell(connectDateStart, 8);
        cells[3] = new Cell(connectDateEnd, 12);
        list.add(new Row(cells));

        list.add(new Row());
        list.add(new Row());

        for (Object o : values) {
            list.add(new Row(getCells(o)));
        }

        return list;
    }

    private Cell[] getCells(Object o) {
        Map<String, String> mapTopic = new HashMap<>();
        mapTopic.put("01", "单选");
        mapTopic.put("02", "多选");
        mapTopic.put("03", "开放");
        Map map = (Map) o;
        Cell[] cells = new Cell[72];
        cells[0] = new Cell(map.get("cusName") == null ? null : map.get("cusName").toString(), 1);
        cells[1] = new Cell(map.get("cusPhone") == null ? null : map.get("cusPhone").toString(), 2);
        for (int i = 0; i < 10; i++) {
            //cells[2 + i * 6] = new Cell(map.get("type" + (i + 1)) == null ? null : map.get("type" + (i + 1)).toString(), 3 + i * 6);
            String type = map.get("type" + (i + 1)) == null ? null : map.get("type" + (i + 1)).toString();
            cells[2 + i * 7] = new Cell(mapTopic.get(type), 3 + i * 7);
            cells[3 + i * 7] = new Cell(map.get("number" + (i + 1)) == null ? null : map.get("number" + (i + 1)).toString(), 4 + i * 7);
            cells[4 + i * 7] = new Cell(map.get("lead" + (i + 1)) == null ? null : map.get("lead" + (i + 1)).toString(), 5 + i * 7);
            cells[5 + i * 7] = new Cell(map.get("stem" + (i + 1)) == null ? null : map.get("stem" + (i + 1)).toString(), 6 + i * 7);
            cells[6 + i * 7] = new Cell(map.get("answer" + (i + 1)) == null ? null : map.get("answer" + (i + 1)).toString(), 7 + i * 7);
            cells[7 + i * 7] = new Cell(map.get("remark" + (i + 1)) == null ? null : map.get("remark" + (i + 1)).toString(), 8 + i * 7);
            cells[8 + i * 7] = new Cell(map.get("forwardEnding" + (i + 1)) == null ? null : map.get("forwardEnding" + (i + 1)).toString(), 9 + i * 7);
        }
        return cells;
    }


    private List getSurveyResult(String[] areas, String version, String connectDateStart, String connectDateEnd) {
        List list = new ArrayList();
        try {
            //调研记录
            List listSurvey = crmRepository.findSurveyResultForGroup(areas, version, connectDateStart, connectDateEnd);
            if (listSurvey != null && !listSurvey.isEmpty()) {
                List<CrmTopicModel> listGroupTopic = crmTopicRepository.findTopicByVersion(GetConfigureInfoUtils.getConfigureInfo("file_group"), version);
                for (int i = 0; i < listSurvey.size(); i++) {
                    Map surveyResult = (Map) listSurvey.get(i);
                    list.add(getData(surveyResult, listGroupTopic));
                }
            }
        } catch (Exception e) {
            logger.error("查询调研结果程序异常", e);
        }
        return list;
    }

    private Map getData(Map surveyResult, List<CrmTopicModel> groupTopic) {
        Map result = new HashMap();

        String topicId = surveyResult.get("topicId") == null ? "" : surveyResult.get("topicId").toString();
        String crm_remark = surveyResult.get("crm_remark") == null ? "" : surveyResult.get("crm_remark").toString();
        String answerId = surveyResult.get("answerId") == null ? "" : surveyResult.get("answerId").toString();
        String forwardEnding = surveyResult.get("forwardEnding") == null ? "" : surveyResult.get("forwardEnding").toString();

        String[] topicIds = topicId.split("___MARK___", -1);
        String[] crm_remarks = crm_remark.split("___MARK___", -1);
        String[] answerIds = answerId.split("___MARK___", -1);
        String[] forwardEndings = forwardEnding.split("___MARK___", -1);

        result.put("cusName", surveyResult.get("cusName") == null ? null : surveyResult.get("cusName").toString());
        result.put("cusPhone", surveyResult.get("cusPhone") == null ? null : surveyResult.get("cusPhone").toString());

        putData(result, groupTopic, topicIds, crm_remarks, answerIds, forwardEndings);
        return result;
    }

    private void putData(Map result, List<CrmTopicModel> dataList, String[] topicIds, String[] crm_remarks, String[] answerIds, String[] forwardEndings) {
        for (int i = 0; i < dataList.size(); i++) {
            CrmTopicModel crmTopicModel = dataList.get(i);
            result.put("type" + (i + 1), crmTopicModel.getType());
            result.put("number" + (i + 1), crmTopicModel.getNumber());
            result.put("lead" + (i + 1), crmTopicModel.getLead());
            result.put("stem" + (i + 1), crmTopicModel.getStem());
            String id = crmTopicModel.getId() + "";
            for (int j = 0; j < topicIds.length; j++) {
                if (id.equals(topicIds[j])) {
                    result.put("remark" + (i + 1), crm_remarks[j]);
                    result.put("answer" + (i + 1), answerIds[j]);
                    result.put("forwardEnding" + (i + 1), forwardEndings[j]);
                }
            }
        }
    }
}
